package com.osell.selection.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.da.lon.wang.xlg.R;
import com.osell.selection.data.network.entity.Product;
import com.osell.selection.ui.base.BaseFragment;
import com.osell.selection.ui.common.WebViewActivity;
import com.osell.selection.ui.product.ProductDetailActivity;
import com.sprylab.android.widget.TextureVideoView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RecommendProductFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.vv_product_video)
    TextureVideoView vv_product_video;
    @BindView(R.id.iv_product_image)
    ImageView iv_product_image;

    private Unbinder mUnBinder;

    private Product mProduct;

    public static RecommendProductFragment newInstance(Product product) {
        RecommendProductFragment fragment = new RecommendProductFragment();
        Bundle args = new Bundle();
        args.putSerializable("product", product);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mProduct = (Product) args.getSerializable("product");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend_product, container, false);
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        vv_product_video.setOnClickListener(this);
        iv_product_image.setOnClickListener(this);

        Glide.with(this)
                .load(mProduct.imageUrl)
                .centerCrop()
                .into(iv_product_image);
        vv_product_video.setOnPreparedListener(mp -> iv_product_image.setVisibility(View.GONE));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mProduct instanceof Product.VideoProduct) {
            if (isVisibleToUser) {
                iv_product_image.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mProduct.videoLink)) {
                    vv_product_video.setVideoPath(mProduct.videoLink);
                    vv_product_video.start();
                }
            } else {
                vv_product_video.stopPlayback();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.vv_product_video:
            case R.id.iv_product_image:
                if (mProduct.webFlag == 1) {
                    v.getContext().startActivity(new Intent(v.getContext(), WebViewActivity.class)
                            .putExtra("url", mProduct.webUrl).putExtra("hide_top_bar", true));
                } else {
                    v.getContext().startActivity(new Intent(v.getContext(), ProductDetailActivity.class)
                            .putExtra("product_number", mProduct.number));
                }
                break;
            default:
                break;
        }
    }
}
